package ch.mustaffah.aoide.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.mustaffah.aoide.player.PerpetuumPlayer;

public class MainApplicationFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private AoideKeyBord aoideKeyBord0;
	private AoideKeyBord aoideKeyBord1;
	private JPanel applicationPanel;
	private JPanel commandPanel;
	private JButton perpetuumButton;
	private PerpetuumPlayer perpetuumPlayer;
	private JComboBox velocitySelectionComboBox;
	private JLabel phraseLenghtLabel;
	private JSlider phraseLengthSlider;
	private JCheckBox randomRhythmCheckBox;


	public JButton getPerpetuumButton() {
		if (this.perpetuumButton == null) {
			this.perpetuumButton = new JButton("play");
			this.perpetuumButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					startPerpetuum();
				}
			});
		}

		return perpetuumButton;
	}
	
	public JComboBox getVelocitySelection() {
		if (this.velocitySelectionComboBox == null) {
			this.velocitySelectionComboBox = new JComboBox(VelocityMode.values());
			this.velocitySelectionComboBox.setSelectedItem(VelocityMode.Allegro);
		}
		return this.velocitySelectionComboBox;
	}

	public MainApplicationFrame() {
		initializeView();
		createGui();
		setVisible(true);
	}

	private void createGui() {
		setContentPane(getMainPanel());
	}

	private Container getMainPanel() {

		if (this.mainPanel == null) {
			this.mainPanel = new JPanel();
			this.mainPanel.add(getApplicationPanel());
		}
		return this.mainPanel;
	}
	

	private Component getApplicationPanel() {
		if (this.applicationPanel == null) {
			this.applicationPanel = new JPanel();
			this.applicationPanel.setLayout(new BoxLayout(
					this.applicationPanel, BoxLayout.X_AXIS));
			this.applicationPanel.add(getAoideKeyBord0());
			this.applicationPanel.add(getAoideKeyBord1());
			this.applicationPanel.add(getCommandPanel());
		}
		return this.applicationPanel;
	}

	private Component getCommandPanel() {
		if (this.commandPanel == null) {
			this.commandPanel = new JPanel();
			this.commandPanel.add(getPerpetuumButton());
			this.commandPanel.add(getVelocitySelection());
			this.commandPanel.add(getPhraseLengthSlider());
			this.commandPanel.add(getPhraseLengthLabel());
			this.commandPanel.add(getRandomRhythmCheckBox());
		}
		return this.commandPanel;
	}
	
	public JCheckBox getRandomRhythmCheckBox() {
		if (this.randomRhythmCheckBox == null) {
			this.randomRhythmCheckBox = new JCheckBox();
			this.randomRhythmCheckBox.setSelected(true);
		}
		return this.randomRhythmCheckBox;
	}

	public JLabel getPhraseLengthLabel() {

		if (this.phraseLenghtLabel == null) {
			this.phraseLenghtLabel = new JLabel("128");
		}
		return this.phraseLenghtLabel;
	}

	public JSlider getPhraseLengthSlider() {
		if (this.phraseLengthSlider == null) {
			this.phraseLengthSlider = new JSlider(JSlider.HORIZONTAL, 1, 256, 128);
			this.phraseLengthSlider.setPreferredSize(new Dimension(100, 50));
			this.phraseLengthSlider.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0) {
					getPhraseLengthLabel().setText(
							"" + phraseLengthSlider.getValue());
				}
			});
		}
		return this.phraseLengthSlider;
	}
	
	public AoideKeyBord getAoideKeyBord0() {
		if (this.aoideKeyBord0 == null) {
			this.aoideKeyBord0 = new AoideKeyBord();
		}
		return this.aoideKeyBord0;
	}

	
	public AoideKeyBord getAoideKeyBord1() {
		if (this.aoideKeyBord1 == null) {
			this.aoideKeyBord1 = new AoideKeyBord();
		}
		return this.aoideKeyBord1;
	}

	private void initializeView() {
		setSize(new Dimension(1100, 600));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new MainApplicationFrame();
	}

	private void startPerpetuum() {
		if (this.perpetuumPlayer == null) {
			this.perpetuumPlayer = new PerpetuumPlayer(this);
			perpetuumPlayer.setDaemon(true);
			perpetuumPlayer.start();
		}else{
			this.perpetuumPlayer.stop();
			this.perpetuumPlayer=null;
		}
	}
}
